<!DOCTYPE html>
<html lang="zh-CN">
{% load static %}
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>注册</title>
	<meta name="keywords" content="" />
	<meta name="description" content="" />
	<link rel="stylesheet" type="text/css" href="{% static 'css/bootstrap.min.css' %}">
    <link rel="shortcut icon" href="{% static 'img/logo.png' %}">
	<link rel="stylesheet" type="text/css" href="{% static 'css/bootstrapValidator.min.css' %}">
    <style>
        body{
            width: 100%;
            height: calc(100vh - 100px);
            background: url("{% static 'img/bg-01.jpg' %}")no-repeat;
            background-size: cover;
        }
        #main{
            position: fixed;
            width: 395px;
            background: #fff;
            border: 1px solid #ccc;
            top: 50%;
            left: 50%;
            margin-top: -240px;
            margin-left: -197.5px;
            box-shadow: 0px 0px 5px #333333;
            -webkit-border-radius: 5px;
            -moz-border-radius: 5px;
            border-radius: 5px;
        }
        .has-feedback .form-control {
            padding-right: 12px;
        }
        .input-group{
            width: 100%;
        }
.login-top {
    font-size: 24px;
    margin-top: 30px;
    padding-left: 40px;
    box-sizing: border-box;
    color: #333333;
    margin-bottom: 20px;
}
.login-center {
    width: 100%;
    box-sizing: border-box;
    padding: 0 40px;
    margin-bottom: 20px!important;
}
.login-button {
    cursor: pointer;
    width: 313px;
    text-align: center;
    height: 40px;
    background-color: dodgerblue;
    border-radius: 5px;
    margin: 0 40px;
    color: white;
}
        .login-button:hover{
            color: white;
        }
    {#    p{#}
    {#       width: 370px;#}
    {#padding: 20px 40px;#}
    {#display: flex;#}
    {#justify-content: space-between;#}
    {#    }#}
        a{
            text-decoration: none;
            color: #333;
        }
        a:hover{
            text-decoration: none;
            color: #333;
        }
        .error{
            font-size: 14px;
            color: red;
           position: relative;
            left: 40px;
            top: -10px;
        }
        #main>p{
            margin: 20px 40px;
        }
        #main>p>.fl{
            float: left;
        }
        #main>p>.fr{
            float: right;
        }
        .error{
            display: block;
        }
    </style>
</head>

<body>
<div id="main">
     <form class="form-inline" id="defaultForm">
         {% csrf_token %}
         <div class="login-top">
				注册
			</div>
      <div class="form-group login-center">

        <div class="input-group">
          <div class="input-group-addon"><i class="glyphicon glyphicon-user"></i></div>
          <input type="text" class="form-control verification" name="username" required placeholder="请输入用户名">
        </div>
      </div>
         <div class="form-group login-center">

        <div class="input-group">
          <div class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></div>
          <input type="email" class="form-control verification" name="email" placeholder="请输入邮箱"required >
        </div>
      </div>
             <div class="form-group login-center">

        <div class="input-group">
          <div class="input-group-addon"style="width: 48px"><i class="glyphicon glyphicon-phone"></i></div>
          <input type="text" class="form-control "required name="captcha" placeholder="请输入验证码"style="width: 50%">
            <img src="" alt="" id="img" style="display: inline;padding:4px 5px;float: right">

        </div>
      </div>
       {% comment %}  <div class="form-group login-center">

        <div class="input-group">
          <div class="input-group-addon"><i class="glyphicon glyphicon-phone"></i></div>
          <input type="text" class="form-control" id="exampleInputAmount" placeholder="请输入手机号"required minlength="11"maxlength="11" >
        </div>
      </div>{% endcomment %}
     {% comment %}    <div class="form-group login-center">

        <div class="input-group">
          <div class="input-group-addon"style="width: 48px"><i class="glyphicon glyphicon-phone"></i></div>
          <input type="tel" class="form-control" id="exampleInputAmount" placeholder="请输入手机验证码"style="width: 62%">
           <button class="btn btn-primary"type="button" style="display: inline;padding:6px 5px" id="zphone">发送验证码</button>
        </div>
      </div>{% endcomment %}
         <input type="hidden" id="hidden">
       <div class="form-group login-center">

        <div class="input-group">
          <div class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></div>
          <input type="password" class="form-control verification" name="password" minlength="6"required placeholder="请输入密码">
        </div>
      </div>
         <div class="form-group login-center">

            <div class="input-group ">
              <div class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></div>
              <input type="password" class="form-control verification" name="password1" minlength="6"required placeholder="请确认密码">
            </div>
      </div>
        <p class="errors"></p>

  <button type="button" class="btn login-button " id="submit">注册</button>
            <div class="qq" style="padding-left: 40px;padding-top: 10px;">
             其他方式注册: <a href="{% url 'qq-login' %}?next={{ next }}"><img style="width: 25px;" src="{% static 'img/qq2.png' %}" alt="QQ登录"></a>
         </div>
</form>
    <p class="clearfix">
        <a class="fl" href="/login">已有账号?立即登录</a>
        <a class="fr" href="{% url 'home' %}">首页</a>
    </p>
</div>

	<script src="{% static 'js/jquery-3.3.1.min.js' %}"></script>
	<script src="{% static 'js/bootstrap.min.js' %}"></script>
	<script src="{% static 'js/bootstrapValidator.min.js' %}"></script>
	<script src="{% static 'js/language/zh_CN.js' %}"></script>
<script src="{% static 'js/jquery.cookie.min.js' %}"></script>
 <script src="{% static 'js/promise.min.js' %}"></script>
    <script src="{% static 'js/sweetalert.min.js' %}"></script>
<script>
       $('#defaultForm')
        .bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                username: {
                    validators: {
                        notEmpty: {
                            message: '不能为空'
                        },
                    {% comment %}    regexp: {
                            regexp: /^[a-zA-Z0-9_\.]+$/,
                            message: 'The username can only consist of alphabetical, number, dot and underscore'
                        }{% endcomment %}
                    }
                },
                email: {
                    validators: {
                        notEmpty: {
                            message: '不能为空'
                        },
                        emailAddress: {
                            message: '请输入正确邮箱'
                        }
                    }
                },
                password: {
                    validators: {
                        notEmpty: {
                            message: '不能为空'
                        },
                        stringLength: {  //长度限制
                          min: 6,
                          message: '密码不能小于6位'
                        },
                         identical: {//相同
                         field: 'password', //需要进行比较的input name值
                         message: '两次密码不一致'
                     },
                    }
                },
                password1: {
                    validators: {
                        notEmpty: {
                            message: '不能为空'
                        },
                          stringLength: {  //长度限制
                          min: 6,
                          message: '密码不能小于6位'
                        },
                         identical: {//相同
                         field: 'password', //需要进行比较的input name值
                         message: '两次密码不一致'
                     },
                    }
                },
                captcha: {
                    validators: {
                        notEmpty: {
                            message: '不能为空'
                        },
                        stringLength: {
                            min: 4,
                            max: 4,
                            message: '请输入四位验证码'
                        },
                        remote: {
                            threshold:4,
                            type: 'POST',
                            url: '/yan/',

                            data:{response:function() {
                              return $("input[name='captcha']").val() },
                                hashkey:function() {
                              return $("#hidden").val() },
                                csrfmiddlewaretoken:function() {
                              return $('input[name="csrfmiddlewaretoken"]').val() },
                          },
                        },
                    }
                },
            }
        })


    function Verification (){
        $.ajax({
             type: "GET",
             url: "/refresh/",
             dataType: "json",
             success: function(data){
                 $('#img').attr('src',data.image_url)
                 $('#hidden').val(data.key)
                       console.log(data)
                      },
             error:function (xht) {
                 console.log(xht)
             }
         });
    }
    Verification()
    $('img').on('click',function () {
        Verification()
    })
   {% comment %}  $("input[name='captcha']").on('focus',function () {
         var self = $(this)
         self.css('border-color','#ccc')
     })
    $("input[name='captcha']").on('blur',function () {
        var self = $(this)
        $.ajax({
             type: "post",
             url: "/yan/",
             dataType: "json",
            data:{"response":self.val(),"hashkey":$("#hidden").val()},
            headers:{'X-CSRFToken':$.cookie('csrftoken')},
             success: function(data){


              },
             error:function (xht) {
                 console.log(xht)
             }
         });
    })
{% endcomment %}
    $('#submit').on('click',function () {

        $("form").bootstrapValidator('validate');//提交验证
                if ($("form").data('bootstrapValidator').isValid()) {//获取验证结果，如果成功，执行下面代码
                    var obj={}
                    $(".verification").each(function (i,item) {
                        obj[$(item).attr('name')]=$(item).val()
                    })
                    $.ajax({
                        url:'/register/',
                        type:'POST',
                        data:obj,
                        headers:{'X-CSRFToken':$.cookie('csrftoken')},
                        success:function (data) {
                            $('.errors').html('');
                            if(data.status==200){
                                  swal({
                                  text: data.message,
                                  icon: "success",
                                })
                            }else{
                                data.data.email?$('.errors').append("<span class='error'>"+data.data.email[0]+"</span>"):''
                                data.data.username?$('.errors').append("<span class='error'>"+data.data.username[0]+"</span>"):''
                                data.data.password?$('.errors').append("<span class='error'>"+data.data.password+"</span>"):''
                            }

                        }

                    })

                }

        console.log(obj)
    })
</script>
{% comment %}<script>
     var iTime = 60;
    sTime = ''
    function RemainTime(){
        if (iTime == 0) {
            document.getElementById('zphone').disabled = false;
            sTime="获取验证码";
            iTime = 60;
            document.getElementById('zphone').innerHTML = sTime;
            return;
        }else{
            document.getElementById('zphone').disabled = true;
            sTime="重新发送(" + iTime + ")";
            iTime--;
        }
        setTimeout(function() { RemainTime() },1000)
        document.getElementById('zphone').innerHTML = sTime;
    }
$('#zphone').on('click',function () {
    RemainTime()
})
    // 检查用户输入的手机号是否合法
    function check_mobile() {

        var re = /^1[345678]\d{9}$/; //校验手机号

        if(re.test($('#mobile').val()))
        {
            $('#mobile').next().hide();
            error_mobile = false;
            document.getElementById('zphone').disabled = false;
        }
        else
        {
            $('#mobile').next().html('你输入的手机格式不正确')
            $('#mobile').next().show();
            error_mobile = true;
            document.getElementById('zphone').disabled = true;
        }
    }

</script>{% endcomment %}
</body>

</html>